package net.guoyixian.admin.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.guoyixian.admin.pojo.SysManagers;
import net.guoyixian.admin.service.SysManagersService;
import net.guoyixian.common.web.DataGrid;
import net.guoyixian.common.web.GyxResult;
import net.guoyixian.common.web.PageUtils;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.Order.Direction;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Controller
// @SessionAttributes("currUser")
// @RequestMapping("user")
public class SysManagersController extends BaseController {

	private static Logger logger = LoggerFactory
			.getLogger(SysManagersController.class);
	@Autowired
	private SysManagersService sysManagersService;

	@RequestMapping("login")
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("login");
		return mv;
	}

	/**
	 * 用户登录
	 * 
	 * @param modelMap
	 * @return ResponseBody注解用于返回json,xml等文本数据
	 */
	@RequestMapping(value = "/user/dologin")
	@ResponseBody
	public GyxResult dologin(@RequestParam("username") String username,
			@RequestParam("password") String password,boolean rememberMe,
			HttpServletRequest request, HttpServletResponse response) {

		Subject shiroUser = SecurityUtils.getSubject();
		password = DigestUtils.md5Hex(password);
		UsernamePasswordToken token = new UsernamePasswordToken(username,
				password);
		token.setRememberMe(rememberMe);

		shiroUser.login(token);
		return GyxResult.ok();

	}

	@RequestMapping(value = "/logout")
	public String logout(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Subject shiroUser = SecurityUtils.getSubject();
		shiroUser.logout();

		// String ticket ="TICKET_"+CookieUtils.getCookieValue(request,
		// UserController.TT_TICKET);
		// // ticket="123";
		//
		// if (StringUtils.isNotBlank(ticket)) {
		// this.userService.logout(ticket);
		// CookieUtils.deleteCookie(request, response,
		// UserController.TT_TICKET);
		// }
		return "login";
		// response.sendRedirect("login");
	}
	
	/**
	 * 页面查询
	 * @return
	 */
	@RequiresPermissions("user:query")
	@RequestMapping("user/list")
	public String list() {
		return "user/userlist";
	}
	
	@RequiresPermissions("user:query")
	@RequestMapping("user/query")
	@ResponseBody
	public DataGrid query(Integer page, Integer rows, String sort, String order, String username, String realname){
		page = (page == null) ? 1 : page;
		rows = rows > 0 ? rows : PageUtils.LIMIT;
		Direction direction = Direction.valueOf(order.toUpperCase(Locale.US));
		Order _order = new Order(PageUtils.LowerCaseWithUnder(sort), direction, null);
		List<Order> orders = new ArrayList<Order>();
		orders.add(_order);
		PageBounds bounds = new PageBounds(page, rows, orders);
		PageList<SysManagers> list = sysManagersService.queryList(bounds, username, realname);
		DataGrid dataGrid = new DataGrid();
		dataGrid.setRows(list);
		dataGrid.setTotal(list.getPaginator().getTotalCount());
		return dataGrid;
	}
	
	/**
	 * 添加用户
	 * @return
	 */
	@RequiresPermissions("user:add")
	@RequestMapping("user/add")
	public String add(){
		return "user/useradd";
	}
	
	@RequiresPermissions("user:add")
	@RequestMapping("user/save")
	public GyxResult save(SysManagers managers){
		sysManagersService.save(managers);
		return GyxResult.build(200, "新增成功！");
	}
	
}